


--use PolePosition

-- // checksum failed custom error
--exec sp_addmessage @msgnum = 505050, @severity = 16, @msgtext = N'%s(%s) checksum compare failed', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // not found custom error
--exec sp_addmessage @msgnum = 505060, @severity = 16, @msgtext = N'%s(%s) does not exist', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // unit test custom error
--exec sp_addmessage @msgnum = 505070, @severity = 16, @msgtext = N'%s Test Failed. Expected< %s > Actual< %s > %s', @replace = 'REPLACE', @lang = 'us_english'
GO



--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'v_tb_Distribution_MeterNotes') and OBJECTPROPERTY(id, N'IsProcedure') = 0)
drop view v_tb_Distribution_MeterNotes
GO

create view v_tb_Distribution_MeterNotes as
	/* 	Initial Generated Date:  Saturday, February 14, 2009 
		Purpose: Selected record(s) from the table; this view is the source for all procedures' select statements
	*/
	
select
	 [MeterNoteId] as [MeterNoteId]
	,[Name] as [Name]
	-- customization may be required for FriendlyName
	,Left(CONVERT(varchar(255), [Name]), 255) as [FriendlyName]
from
	tb_Distribution_MeterNotes
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_DistributionMeterNote_Test') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_DistributionMeterNote_Test
GO


create proc up_DistributionMeterNote_Test
	@MeterNoteId int
as
	/* 	Initial Generated Date:  Saturday, February 14, 2009 
		Purpose: Test if a record exists, using the primary key
	*/

SET NOCOUNT ON	
	
if exists(
	select [MeterNoteId] 
	from [tb_Distribution_MeterNotes] source
	where [source].[MeterNoteId] = @MeterNoteId)
begin
	return(1)
end
else
begin
	return(0)
end
GO






--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_DistributionMeterNote_Select') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_DistributionMeterNote_Select
GO

create proc up_DistributionMeterNote_Select
	 @MeterNoteId int = null 
	,@Name nvarchar(50) = null 
as
	/* 	Initial Generated Date:  Saturday, February 14, 2009 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

select * from v_tb_Distribution_MeterNotes
where
		IsNull([v_tb_Distribution_MeterNotes].[MeterNoteId], -1) = Coalesce(@MeterNoteId, [v_tb_Distribution_MeterNotes].[MeterNoteId], -1)
	AND	IsNull([v_tb_Distribution_MeterNotes].[Name], '') = Coalesce(@Name, [v_tb_Distribution_MeterNotes].[Name], '')
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_DistributionMeterNote_Select_Top1') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_DistributionMeterNote_Select_Top1
GO

create proc up_DistributionMeterNote_Select_Top1
	 @MeterNoteId int = null 
	,@Name nvarchar(50) = null 
as
	/* 	Initial Generated Date:  Saturday, February 14, 2009 
		Purpose: Selected top 1 record based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

select TOP 1 * from v_tb_Distribution_MeterNotes
where
		IsNull([v_tb_Distribution_MeterNotes].[MeterNoteId], -1) = Coalesce(@MeterNoteId, [v_tb_Distribution_MeterNotes].[MeterNoteId], -1)
	AND	IsNull([v_tb_Distribution_MeterNotes].[Name], '') = Coalesce(@Name, [v_tb_Distribution_MeterNotes].[Name], '')
GO




--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_DistributionMeterNote_Insert') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_DistributionMeterNote_Insert
GO

create proc up_DistributionMeterNote_Insert
	 @MeterNoteId int =null OUTPUT  
	,@Name nvarchar(50)  
as
	/* 	Initial Generated Date:  Saturday, February 14, 2009 
		Purpose: Inserts a new record; missing arguments are set as their default (typically null)
	*/

SET NOCOUNT ON	
--int

insert tb_Distribution_MeterNotes 
	(
	
	 [Name] 
	)
	values
	(
	
	 @Name 
	)
set @MeterNoteId = scope_identity()
select * from v_tb_Distribution_MeterNotes where MeterNoteId = @MeterNoteId

GO




--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_DistributionMeterNote_Update') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_DistributionMeterNote_Update
GO


create proc up_DistributionMeterNote_Update
	 @MeterNoteId int  
	,@Name nvarchar(50) = null  
	,@OriginalMeterNoteId int  = null --Original PrimaryKey
	
as
	/* 	Initial Generated Date:  Saturday, February 14, 2009 
		Purpose: Updates an existing record, verifying exist; missing arguments persist original record value
		NOTE: this version of the update allows up to write nulls to the table.
	*/

SET NOCOUNT ON	

if @OriginalMeterNoteId is null
	set @OriginalMeterNoteId = @MeterNoteId

-- test
declare @Result bit
execute @Result = up_DistributionMeterNote_Test @OriginalMeterNoteId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @MeterNoteId)
	raiserror(505060, 16, 1, 'DistributionMeterNote', @x)
	return
end

-- proceed
update tb_Distribution_MeterNotes 
set

	 [Name] = IsNull(@Name, [source].[Name])
	
from tb_Distribution_MeterNotes source
where
	[source].[MeterNoteId] = @OriginalMeterNoteId


select * from v_tb_Distribution_MeterNotes where MeterNoteId = @MeterNoteId
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_DistributionMeterNote_Delete') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_DistributionMeterNote_Delete
GO


create proc up_DistributionMeterNote_Delete
	@MeterNoteId int
as
	/* 	Initial Generated Date:  Saturday, February 14, 2009 
		Purpose: Deletes an existing record, verifying exist
	*/

SET NOCOUNT ON	

-- test
declare @Result bit
execute @Result = up_DistributionMeterNote_Test @MeterNoteId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @MeterNoteId)
	raiserror(505060, 16, 1, 'DistributionMeterNote', @x)
	return
end

-- proceed
delete from tb_Distribution_MeterNotes 
where [MeterNoteId] = @MeterNoteId

GO


